<template>
  <div>
    <h2>Home</h2>
    <!-- <el-row>
      <el-col :span="22"><el-input></el-input></el-col>
      <el-col :span="2">
        <el-button type="primary">添加</el-button>
      </el-col>
    </el-row> -->
    <div>
      <el-button :type="active === 0 ? 'primary' : ''" @click="search(0)">全部</el-button>
      <el-button :type="active === 1 ? 'primary' : ''" @click="search(1)">未完成</el-button>
      <el-button :type="active === 2 ? 'primary' : ''" @click="search(2)">进行中</el-button>
      <el-button :type="active === 3 ? 'primary' : ''" @click="search(3)">已完成</el-button>
    </div>
    <div>
      <el-input type="text" v-model="title">
        <template #prefix>
          <div>
            标题
          </div>
        </template>
        <template #append>
          <div>
            <el-button @click="add">添加</el-button>
          </div>
        </template>
      </el-input>
    </div>
    <el-table :data="shopStore.lists" :row-class-name="tableRowClassName">
      <el-table-column prop="title" label="标题" width="200"></el-table-column>
      <el-table-column label="状态">
        <template #default="scope">
          {{ shopStore.status[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column label="删除">
        <template #default="scope">
          <el-button link type="danger" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
      <el-table-column label="修改状态">
        <template #default="scope">
          <el-button @click="update(scope.row.id, 1)">未完成</el-button>
          <el-button @click="update(scope.row.id, 2)">进行中</el-button>
          <el-button @click="update(scope.row.id, 3)">已完成</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script setup>
import { ElMessageBox } from 'element-plus'

import { ref } from "vue";
import { useShopStore } from "../store/s1.js";

const shopStore = useShopStore();

const title = ref('');
const active = ref(0);

const tableRowClassName = function (obj) {
  const status = {
    1: 's1',
    2: 's2',
    3: 's3'
  }
  return status[obj.row.status];
}

// 查询过滤

const search = async function (status) {
  active.value = status;
  await shopStore.searchByStatus(status);
}

// 更新
const update = async function (id, status) {
  await shopStore.updateStatusList(id, status);
}


// 删除
const del = async function (id) {
  try {
    await ElMessageBox.confirm('确定删除吗?', '提示');
    // 删除
    await shopStore.delList(id);
  } catch (error) { }
};

// 添加

const add = async function () {
  if (title.value === '') return;
  try {
    await shopStore.addList({ title: title.value, status: 1 });
    title.value = '';
  } catch (error) {
    console.log(error);
  }
};

</script>
<style scoped lang="scss">
// 修改框架样式的时候，一般在scoped模式下都需要使用样式穿透
:deep(.el-table) tr {
  &.s1 {
    background: var(--el-color-danger-light-9);
  }

  &.s2 {
    background: var(--el-color-warning-light-9);
  }

  &.s3 {
    background: var(--el-color-success-light-9);
  }
}
</style>